/*******************************************************************************

  Pilot Intelligence Library
    https://gitee.com/pi-lab

*******************************************************************************/


#pragma once

#include <string>
#include <memory>


namespace pi {

class ComManager_InnerData;

class ComManager
{
public:
    ComManager();
    virtual ~ComManager();

    /**
     * @brief open a communication device (UART, TCP, UDP)
     *
     * @param com_name - communication name, the define is listed at
     *      https://ardupilot.org/mavproxy/docs/getting_started/starting.html
     *  examples:
     *      /dev/ttyUSB0
     *      /dev/ttyUSB0,57600
     *      COM14
     *      COM14,115200
     *      tcp:192.168.1.1:14550
     *      tcpclient:192.168.241.110:14520
     *      udp:192.168.1.1:14550
     *      udpclient:192.168.241.110:14520
     *      udpmultcast:225.0.0.211:14550
     *
     * @return
     *      0 - success
     *      other - failed
     */
    int open(const std::string& com_name, const std::string& out_name = "127.0.0.1:14550");

    /**
     * @brief close the communication device
     *
     * @return
     */
    int close(void);

    /**
     * @brief return communication device is open or not
     * @return
     */
    int isOpen(void);


    /**
     * @brief write data to communication device
     *
     * @param d     - data buffer
     * @param len   - data lenght
     * @return
     *      actuall write data length
     */
    int write(char *d, int len);

    /**
     * @brief read data from communication device
     *
     * @param d     - data buffer
     * @param len   - max read data length
     *
     * @return
     *      actuall read data length
     */
    int read(char *d,  int len);

protected:
    std::shared_ptr<ComManager_InnerData>   m_priData;
};

} // end of namespace pi
